﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for DDLYear
/// </summary>
/// 
namespace Web.UI.CustomControls
{
    public class YearDropDownList : DropDownList
    {
        private int selectedYear;

        public int SelectedYear
        {
            get
            {
                if (this.selectedYear<0)
                {
                    try
                    {
                        this.selectedYear = Convert.ToInt32(this.SelectedValue);
                    }
                    catch(FormatException ex)
                    {
                        throw new FormatException("The year must me an integer value.",ex);
                    }
                }

                return this.selectedYear;
            }
            set
            {

                this.selectedYear = value;
                this.SelectedValue = value.ToString();
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!this.Page.IsPostBack)
            {

                int currentYear = DateTime.Now.Year;
                for(int lowerBoundary = currentYear - 50; lowerBoundary<currentYear+50; lowerBoundary++)
                {
                    this.Items.Add(new ListItem(lowerBoundary.ToString(), lowerBoundary.ToString()));
                }
                this.SelectedValue = currentYear.ToString();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
    }
    
}